package com.example.springtest.aspect;

import com.common.myspring.annotation.LogAnnotation;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;

import java.lang.reflect.Method;

/**
 * @title: CarAspect
 * @Author xu-xiang
 * @Date: 2023/8/30 10:59
 * @description:
 */
@Aspect
//@Component
public class CarAspect {

    @Pointcut("@annotation(com.common.myspring.annotation.LogAnnotation)")
    public void pointCut() {
    }

    @Around("pointCut()")
    public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        Method method = signature.getMethod();

        if (!method.isAnnotationPresent(LogAnnotation.class)) {
            return joinPoint.proceed();
        }

        System.out.println("springAop方法" + method.getName() + "执行开始!");
        Object proceed = joinPoint.proceed();
        System.out.println("springAop方法" + method.getName() + "执行结束!\n");
        return proceed;
    }
}
